fix(ds4): memory spike in sparse pooled attention at 4k+ context#17
Merged
Blaizzy merged 1 commit intoBlaizzy:pc/add-deepseekv4flash-modelfrom Apr 27, 2026
Merged
Conversation
The element-wise (q * pooled).sum() path broadcasts a (B,H,L,1,D) tensor against (B,1,L,topk,D), creating a (B,H,L,topk,D) intermediate. At 4k context with H=64, topk=512, D=512 this is ~137 GB per operation (x2). Replace with equivalent matmul: (B,L,H,D) @ (B,L,D,topk) which produces the (B,L,H,topk) result directly with ~0.25 GB peak memory.
|
And it's faster! 42tps! maybe this matmul is using Neural Accelerator! |
dd6b92f
into
Blaizzy:pc/add-deepseekv4flash-model
2 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
_sparse_pooled_attention(B, H, L, topk, D)intermediate — at 4k context with H=64, topk=512, D=512 this is ~137 GB per operation (×2)(B, L, H, D) @ (B, L, D, topk)using ~0.25 GBContext
From benchmarks on M3 Ultra (512 GB):
The KV cache barely grows, so the spike is entirely from intermediate tensors in
_sparse_pooled_attentionwhich runs during prefill (L > 1) on compress_ratio=4 layers with the indexer.PR: ml-explore#1192
Issue: ml-explore#1192 (comment)
Test plan